package com.antgroup.seckill.business.goods.service.impl;

import com.antgroup.seckill.business.common.enums.RespBeanEnum;
import com.antgroup.seckill.business.common.exception.GlobalException;
import com.antgroup.seckill.business.common.vo.request.UserLoginRequest;
import com.antgroup.seckill.business.common.vo.response.RespBean;
import com.antgroup.seckill.business.goods.mapper.UserMapper;
import com.antgroup.seckill.business.goods.model.User;
import com.antgroup.seckill.business.goods.service.IUserService;
import com.antgroup.seckill.business.utils.CookieUtil;
import com.antgroup.seckill.business.utils.MD5Util;
import com.antgroup.seckill.business.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.antgroup.seckill.business.common.constants.CommonConstant.SALT;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangxy
 * @since 2024-07-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录校验
     *
     * @param userLoginRequest
     * @param request
     * @param response
     * @return
     */
    @Override
    public RespBean doLogin(UserLoginRequest userLoginRequest, HttpServletRequest request, HttpServletResponse response) {



        String nickName = userLoginRequest.getNickName();
        String password = userLoginRequest.getPassword();

        // 密码在做一次md5
        String fromPassToDBPass = MD5Util.fromPassToDBPass(password, SALT);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("nick_name", nickName).eq("password",fromPassToDBPass);
        User user = userMapper.selectOne(queryWrapper);

        //测试异常抛出
        if(Objects.isNull(user)) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        if(Objects.nonNull(user)) {
            //生成cookie
            String cookieUUid = UUIDUtil.uuid();
            //测试放session
            //request.getSession().setAttribute(cookieUuid,user);

            //把用户信息放入redis
            redisTemplate.opsForValue().set("user:" + cookieUUid,user,30, TimeUnit.HOURS);
            CookieUtil.setCookie(response,"userCookie",cookieUUid,7 * 24 * 60 * 60);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.LOGIN_ERROR);
    }


    /**
     * 通过redis获取用户信息
     *
     * @param request
     * @param response
     * @param cookieUUid
     * @return
     */
    @Override
    public User getUserByCookie(HttpServletRequest request, HttpServletResponse response, String cookieUUid) {

        if(StringUtils.isEmpty(cookieUUid)) {
            return null;
        }
        User user = (User)redisTemplate.opsForValue().get("user:" + cookieUUid);

        if(Objects.nonNull(user)) {
            CookieUtil.setCookie(response,"userCookie",cookieUUid,7 * 24 * 60 * 60);
        }

        return user;
    }

}
